package game

import (
	"MyWuXiaGame/server/src/csvs"
	//"encoding/json"
	"fmt"
	"github.com/gorilla/websocket"
	"os"
)

const (
	MOD_PLAYER     = "player"
	MOD_ICON       = "icon"
	MOD_BAG        = "bag"
	MOD_WEAPON     = "weapon"
	MOD_MAP        = "map"
	MOD_MAIL 	   = "mail"
	MOD_NPC		   = "npc"
	MOD_SKILL	   = "skill"
)

type ModBase interface {
	LoadData(player *Player)
	SaveData()
	InitData()
}

type Player struct {
	UserId       int64
	modManage    map[string]ModBase
	localPath    string
	ws           *websocket.Conn
	exitTime     int64
	channelLogic chan []byte
}

var player *Player

func NewTestPlayer(ws *websocket.Conn, userid int64) *Player {
	//***************泛型架构***************************
	player = new(Player)
	player.UserId = userid
	player.channelLogic = make(chan []byte)
	player.modManage = map[string]ModBase{
		MOD_PLAYER:     new(ModPlayer),
		MOD_ICON:       new(ModIcon),
		MOD_BAG:        new(ModBag),
		MOD_WEAPON:     new(ModWeapon),
		MOD_MAP:        new(ModMap),
		MOD_MAIL:		new(ModMail),
		MOD_NPC:		new(ModNpc),
		MOD_SKILL:		new(ModSkill),
	}
	player.InitData()
	player.InitMod()
	player.ws = ws
	return player
}

func (self *Player) InitData() {
	path := GetServer().Config.LocalSavePath
	_, err := os.Stat(path)
	if err != nil {
		err = os.Mkdir(path, os.ModePerm)
		if err != nil {
			return
		}
	}
	self.localPath = path + fmt.Sprintf("/%d", self.UserId)
	_, err = os.Stat(self.localPath)
	if err != nil {
		err = os.Mkdir(self.localPath, os.ModePerm)
		if err != nil {
			return
		}
	}
}

func (self *Player) InitMod() {
	for _, v := range self.modManage {
		v.LoadData(self)
	}
}

func (self *Player) Run() {
	fmt.Println("欢迎来到武侠世界")
	fmt.Println("作者:江湖小虾米李佳斐")
	fmt.Println("模拟用户创建成功OK------开始测试")
	fmt.Println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓")

	for {
		//上线重算属性
		self.GetMod(MOD_PLAYER).(*ModPlayer).ReCalcAbilitys()
		fmt.Println(self.GetMod(MOD_PLAYER).(*ModPlayer).Name, ",欢迎来到武侠世界,请选择功能：1基础信息2背包3地图4武器5邮件6Npc7江湖历练8武学9存储数据")
		var modChoose int
		fmt.Scan(&modChoose)
		switch modChoose {
		case 1:
			self.HandleBase()
		case 2:
			self.HandleBag()
		case 3:
			self.HandleMap()
		case 4:
			self.HandleWeapon()
		case 5:
			self.HandleMail()
		case 6:
			self.HandleNpc()
		case 7:
			self.HandleAddExp()
		case 8:
			self.HandleSkill()
		case 9:
			for _, v := range self.modManage {
				v.SaveData()
			}
		}
	}
}

//基础信息
func (self *Player) HandleBase() {
	for {
		fmt.Println("当前处于基础信息界面,请选择操作：0返回1查询信息2设置名字3头像")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		case 1:
			self.HandleBaseGetInfo()
		case 2:
			self.HandleBagSetName()
		case 3:
			self.HandleBagSetIcon()
		}
	}
}

func (self *Player) HandleBaseGetInfo() {
	fmt.Println("名字:", self.GetMod(MOD_PLAYER).(*ModPlayer).Name)
	fmt.Println("等级:", self.GetMod(MOD_PLAYER).(*ModPlayer).CurLevel())

	if self.GetMod(MOD_PLAYER).(*ModPlayer).Icon == 0 {
		fmt.Println("头像:", "未设置")
	} else {
		fmt.Println("头像:", csvs.GetItemConfig(self.GetMod(MOD_PLAYER).(*ModPlayer).Icon), self.GetMod(MOD_PLAYER).(*ModPlayer).Icon)
	}

	fmt.Println("当前血量:", self.GetMod(MOD_PLAYER).(*ModPlayer).CurHP(), "最大血量:", self.GetMod(MOD_PLAYER).(*ModPlayer).GetAttr(csvs.ATTR_MAXHP))
	fmt.Println("当前蓝量:", self.GetMod(MOD_PLAYER).(*ModPlayer).CurMP(), "最大蓝量:", self.GetMod(MOD_PLAYER).(*ModPlayer).GetAttr(csvs.ATTR_MAXMP))

	fmt.Println("当前属性:")
	for idx, v := range self.GetMod(MOD_PLAYER).(*ModPlayer).AttrInfo {
		config := csvs.GetJobAttributeConfigByIndex(idx)
		if config != nil {
			fmt.Println("属性名:", config.Name, "属性值:", v)
		}
	}
}

func (self *Player) HandleBagSetName() {
	fmt.Println("请输入名字:")
	var name string
	fmt.Scan(&name)
	self.RecvSetName(name)
}

func (self *Player) HandleBagSetIcon() {
	for {
		fmt.Println("当前处于基础信息--头像界面,请选择操作：0返回1查询头像背包2设置头像")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		case 1:
			self.HandleBagSetIconGetInfo()
		case 2:
			self.HandleBagSetIconSet()
		}
	}
}

func (self *Player) HandleBagSetIconGetInfo() {
	fmt.Println("当前拥有头像如下:")
	for _, v := range self.GetModIcon().IconInfo {
		config := csvs.GetItemConfig(v.IconId)
		if config != nil {
			fmt.Println(config.ItemName, ":", config.ItemId)
		}
	}
}

func (self *Player) HandleBagSetIconSet() {
	fmt.Println("请输入头像id:")
	var icon int
	fmt.Scan(&icon)
	self.RecvSetIcon(icon)
}

//背包
func (self *Player) HandleBag() {
	for {
		fmt.Println("当前处于基础信息界面,请选择操作：0返回1增加物品2扣除物品3使用物品4查询背包")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		case 1:
			self.HandleBagAddItem()
		case 2:
			self.HandleBagRemoveItem()
		case 3:
			self.HandleBagUseItem()
		case 4:
			self.GetModBag().HandleBagSendAllItemInfo()
		}
	}
}

func (self *Player) HandleBagAddItem() {
	itemId := 0
	itemNum := 0
	fmt.Println("物品ID")
	fmt.Scan(&itemId)
	fmt.Println("物品数量")
	fmt.Scan(&itemNum)
	self.GetModBag().AddItem(itemId, int64(itemNum))
}

func (self *Player) HandleBagRemoveItem() {
	itemId := 0
	itemNum := 0
	fmt.Println("物品ID")
	fmt.Scan(&itemId)
	fmt.Println("物品数量")
	fmt.Scan(&itemNum)
	self.GetModBag().RemoveItemToBag(itemId, int64(itemNum))
}

func (self *Player) HandleBagUseItem() {
	itemId := 0
	itemNum := 0
	fmt.Println("物品ID")
	fmt.Scan(&itemId)
	fmt.Println("物品数量")
	fmt.Scan(&itemNum)
	self.GetModBag().UseItem(itemId, int64(itemNum))
}

//地图
func (self *Player) HandleMap() {
	fmt.Println("欢迎来到中原武林！")
	for {
		fmt.Println("请选择互动地图1新手村2武当山3少林寺4玉女峰5魔教总坛6绝情谷")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		default:
			self.HandleMapIn(action)
		}
	}
}

func (self *Player) HandleMapIn(mapId int) {
	config := csvs.ConfigMapMap[mapId]
	if config == nil {
		fmt.Println("无法识别的地图")
		return
	}
	self.GetModMap().RefreshByPlayer(mapId)
	self.GetModPlayer().CurMapId = mapId
	for {
		self.GetModMap().GetEventList(config)
		fmt.Println("请选择触发事件Id(0返回)")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		default:
			eventConfig := csvs.ConfigMapEventMap[action]
			if eventConfig == nil {
				fmt.Println("无法识别的事件")
				break
			}
			self.GetModMap().SetEventState(mapId, eventConfig.EventId, csvs.EVENT_END, self)
		}
	}
}

//武器
func (self *Player) HandleWeapon() {
	for {
		fmt.Println("当前处于武器界面，选择功能0返回1查询武器2穿戴武器3脱下武器4强化测试5突破测试")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		//case 1:
		//	self.HandleWeaponUp()
		//case 2:
		//	self.HandleWeaponStarUp()
		//case 3:
		//	self.HandleWeaponRefineUp()
		case 1:
			self.HandleViewWeapon()
		case 2:
			self.HandleWearWeapon()
		case 3:
			self.HandleTakeOffWeapon()
		default:
			fmt.Println("无法识别在操作")
		}
	}
}

func (self *Player) HandleViewWeapon() {
	for _, v := range self.GetModWeapon().WeaponInfo {
		itemConfig := csvs.GetItemConfig(v.WeaponId)
		if itemConfig == nil {
			continue
		}
		fmt.Println("当前拥有的武器：", "武器id：", v.WeaponId, "武器名字：", itemConfig.ItemName, "武器key值：", v.KeyId)
	}
	fmt.Println("当前穿戴的武器：", "武器key值：", self.GetModPlayer().WeaponInfo)
}

func (self *Player) HandleWearWeapon() {
	for {
		fmt.Println("输入需要穿戴的武器key:,0返回")
		var weaponKey int
		fmt.Scan(&weaponKey)
		if weaponKey == 0 {
			return
		}
		weaponInfo := self.GetModWeapon().WeaponInfo[weaponKey]
		if weaponInfo == nil {
			fmt.Println("武器不存在")
			continue
		}
		self.GetModPlayer().WearWeapon(weaponInfo, self)
		fmt.Println("当前穿戴的武器：", "武器key值：", self.GetModPlayer().WeaponInfo)
	}
}

func (self *Player) HandleTakeOffWeapon() {
	if self.GetModPlayer().WeaponInfo == 0 {
		fmt.Println("武器不存在")
		return
	}
	self.GetModPlayer().TakeOffWeapon(self)
	fmt.Println("当前穿戴的武器：", "武器key值：", self.GetModPlayer().WeaponInfo)
}

func (self *Player) HandleMail() {
	for {
		fmt.Println("当前处于邮件操作界面，假定玩家是默认不在线的，上线时加载本地邮件数据，并从邮件管理单元拉取离线邮件数据，拉取来的数据保存在玩家本地")
		fmt.Println("选择功能0返回1上线拉取离线邮件数据2发送邮件3删除邮件4全部删除邮件5展示全部邮件")
		var action int
		fmt.Scan(&action)
		ModMail := self.GetModMail()
		if ModMail != nil {
			switch action {
			case 0:
				return
			case 1:
				ModMail.HandleGetOffLineMail()
			case 2:
				ModMail.HandleSendMailToMailEngine()
			case 3:
				ModMail.HandleDeleteMail()
			case 4:
				ModMail.HandleDeleteAllMail()
			case 5:
				ModMail.HandleShowAllMail()
			default:
				fmt.Println("无法识别在操作")
			}
		}else {
			fmt.Println("邮件模块不存在")
		}
	}
}

func (self *Player) HandleNpc() {
	for {
		fmt.Println("选择功能0返回1查看当前可互动Npc 2和npc互动")
		var action int
		fmt.Scan(&action)

		switch action {
		case 0:
			return
		case 1:
			self.HandleViewAllNpc()
		case 2:
			self.HandleInteractWithNpc()
		}
	}
}

func (self *Player) HandleViewAllNpc() {
	MapId := self.GetModPlayer().CurMapId
	if MapId == 0 {
		fmt.Println("您正处于一片混沌之中，请选择地图，然后进入您想前往的地图")
		return
	} else {
		fmt.Println("您当前所处地图为：", csvs.GetMapName(MapId))
		fmt.Println("您当前可互动的Npc为：")
		for _, v := range csvs.ConfigNpcSlice {
			if v.NpcMapId == MapId {
				fmt.Println(v.NpcId, v.NpcName)
			}
		}
	}
}

func (self *Player) HandleInteractWithNpc() {
	for {
		fmt.Println("选择需要互动的npcid")
		var NpcId int
		fmt.Scan(&NpcId)
		config := csvs.GetNpcConfig(NpcId)
		if config == nil {
			fmt.Println("npc不存在")
			return
		}
		fmt.Println("选择互动方式0返回1在一旁偷偷观察Npc 2和npc闲聊 3和npc切磋")
		var action int
		fmt.Scan(&action)
		switch action {
		case 0:
			return
		case 1:
			self.GetModNpc().ObserveNpc(NpcId)
		case 2:
			self.GetModNpc().ChatWithNpc(NpcId)
		case 3:
			self.GetModNpc().BattleWithNpc(NpcId)
		}
	}
}

func (self *Player) HandleAddExp() {
	for {
		fmt.Println("请选择历练天数，一天增加100exp 0返回")
		var days int
		fmt.Scan(&days)

		if days == 0 {
			return
		}
		self.GetModPlayer().AddExp(days*100, self)
	}
}

func (self *Player) HandleSkill() {
	for {
		fmt.Println("选择功能0返回1查看当前已学会武学 2学习新的武学 3练武")
		var action int
		fmt.Scan(&action)

		ModSkill := self.GetModSkill()
		if ModSkill != nil {
			switch action {
			case 0:
				return
			case 1:
				ModSkill.HandleViewAllSkill()
			case 2:
				ModSkill.HandleLearnSkill()
			case 3:
				ModSkill.HandleTrainSkill()
			default:
				fmt.Println("无法识别在操作")
			}
		}else {
			fmt.Println("技能模块不存在")
		}
	}
}

func (self *Player) GetMod(modName string) ModBase {
	return self.modManage[modName]
}

func (self *Player) GetModPlayer() *ModPlayer {
	return self.modManage[MOD_PLAYER].(*ModPlayer)
}

func (self *Player) GetModIcon() *ModIcon {
	return self.modManage[MOD_ICON].(*ModIcon)
}

func (self *Player) GetModBag() *ModBag {
	return self.modManage[MOD_BAG].(*ModBag)
}

func (self *Player) GetModWeapon() *ModWeapon {
	return self.modManage[MOD_WEAPON].(*ModWeapon)
}

func (self *Player) GetModMap() *ModMap {
	return self.modManage[MOD_MAP].(*ModMap)
}

func (self *Player) GetModMail() *ModMail {
	return self.modManage[MOD_MAIL].(*ModMail)
}

func (self *Player) GetModNpc() *ModNpc {
	return self.modManage[MOD_NPC].(*ModNpc)
}

func (self *Player) GetModSkill() *ModSkill {
	return self.modManage[MOD_SKILL].(*ModSkill)
}

//对外接口
func (self *Player) RecvSetName(name string) {
	self.GetMod(MOD_PLAYER).(*ModPlayer).SetName(name)
}

func (self *Player) RecvSetIcon(iconId int) {
	self.GetMod(MOD_PLAYER).(*ModPlayer).SetIcon(iconId)
}